package com.szlizarrrr.demo.optional;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Async;

public interface CustomerRepository extends JpaRepository<Customer, Long> {

	Optional<Customer> findById(Long id);

	<S extends Customer> S save(S customer);

	Optional<Customer> findByLastname(String lastname);

	default Optional<Customer> findByLastname(Customer customer) {
		return findByLastname(customer == null ? null : customer.lastname);
	}

	@Query("select c from Customer c")
	Stream<Customer> streamAllCustomers();
	
	Stream<Customer> findAllByLastnameIsNotNull();
	
	@Async
	CompletableFuture<List<Customer>> readAllBy();

}

